package com.xbongbong.config;

import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.parent.interfaces.BusinessDetailBaseInfoService;
import com.xbongbong.parent.interfaces.BusinessDetailTopInfoService;
import com.xbongbong.parent.interfaces.BusinessEditPageRenderService;
import com.xbongbong.parent.interfaces.BusinessListService;
import com.xbongbong.parent.interfaces.BusinessNewPageRenderService;
import com.xbongbong.parent.interfaces.BusinessSubFormListService;
import com.xbongbong.parent.interfaces.FormDataService;
import com.xbongbong.parent.interfaces.FormDataUpdateService;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 吴峰
 * @date 2021/12/22 20:53
 */
@Configuration
public class BusinessOperateConfig implements ApplicationContextAware {

    @Autowired
    private ApplicationContext applicationContext;

    private Map<String, BusinessListService> businessListMap;
    @Autowired
    private Map<String, BusinessDetailTopInfoService> businessDetailTopInfoMap;
    @Autowired
    private Map<String, BusinessDetailBaseInfoService> businessDetailBaseInfoMap;
    @Autowired
    private Map<String, BusinessNewPageRenderService> businessNewPageRenderMap;
    @Autowired
    private Map<String, BusinessEditPageRenderService> businessEditPageRenderMap;

    /**
     * 增加@Lazy，避免启动报错，因为process的module扫描不到FormDataService下的实现类
     */
    @Autowired
    @Lazy
    private Map<String, FormDataService> businessAddMap;

    /**
     * 增加@Lazy，避免启动报错，因为process的module扫描不到FormDataUpdateService下的实现类
     */
    @Autowired
    @Lazy
    private Map<String, FormDataUpdateService> businessUpdateMap;

    @Autowired
    private Map<String, BusinessSubFormListService> businessSubFormListMap;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        businessListMap = applicationContext.getBeansOfType(BusinessListService.class);
        businessDetailTopInfoMap = applicationContext.getBeansOfType(BusinessDetailTopInfoService.class);
        businessDetailBaseInfoMap = applicationContext.getBeansOfType(BusinessDetailBaseInfoService.class);
        businessNewPageRenderMap = applicationContext.getBeansOfType(BusinessNewPageRenderService.class);
        businessEditPageRenderMap = applicationContext.getBeansOfType(BusinessEditPageRenderService.class);

        businessAddMap = applicationContext.getBeansOfType(FormDataService.class);
        businessUpdateMap = applicationContext.getBeansOfType(FormDataUpdateService.class);
    }
    /**
     * 列表
     * @return
     */
    @Bean("businessSubFormListMap")
    public Map<Integer, BusinessSubFormListService> businessSubFormListServiceMap() {
        Map<Integer, BusinessSubFormListService> resultMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        if (businessSubFormListMap ==null || businessSubFormListMap.isEmpty()) {
            businessSubFormListMap = applicationContext.getBeansOfType(BusinessSubFormListService.class);
        }
        this.businessSubFormListMap.forEach((key, item)->{
            item.businessType().forEach(businessType->{
                resultMap.put(Integer.valueOf(businessType.toString()), item);
            });
        });
        return resultMap;
    }

    /**
     * 列表
     * @return
     */
    @Bean("businessListMap")
    public Map<Integer, BusinessListService> businessListMap() {
        Map<Integer, BusinessListService> resultMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        if (businessListMap ==null || businessListMap.isEmpty()) {
            businessListMap = applicationContext.getBeansOfType(BusinessListService.class);
        }
        this.businessListMap.forEach((key, item)->{
            item.businessType().forEach(businessType->{
                resultMap.put(businessType, item);
            });
        });
        return resultMap;
    }

    /**
     * 详情
     * @return
     */
    @Bean("businessDetailTopInfoMap")
    public Map<Integer, BusinessDetailTopInfoService> businessDetailMap() {
        Map<Integer, BusinessDetailTopInfoService> resultMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        this.businessDetailTopInfoMap.forEach((key, item)->{
            item.businessType().forEach(businessType->{
                resultMap.put(Integer.valueOf(businessType.toString()), item);
            });
        });
        return resultMap;
    }

    /**
     * 详情 “基本详情”
     * @return
     */
    @Bean("businessDetailBaseInfoMap")
    public Map<Integer, BusinessDetailBaseInfoService> businessDetailBaseInfoMap() {
        Map<Integer, BusinessDetailBaseInfoService> resultMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        this.businessDetailBaseInfoMap.forEach((key, item)->{
            item.businessType().forEach(businessType->{
                resultMap.put(Integer.valueOf(businessType.toString()), item);
            });
        });
        return resultMap;
    }
    /**
     * 新建页面渲染
     * @return
     */
    @Bean("businessNewPageRenderMap")
    public Map<Integer, BusinessNewPageRenderService> businessNewPageRenderMap() {
        Map<Integer, BusinessNewPageRenderService> resultMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        this.businessNewPageRenderMap.forEach((key, item)->{
            item.businessType().forEach(businessType->{
                resultMap.put(Integer.valueOf(businessType.toString()), item);
            });
        });
        return resultMap;
    }
    /**
     * 编辑页面渲染
     * @return
     */
    @Bean("businessEditPageRenderMap")
    public Map<Integer, BusinessEditPageRenderService> businessEditPageRenderMap() {
        Map<Integer, BusinessEditPageRenderService> resultMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        this.businessEditPageRenderMap.forEach((key, item)->{
            item.businessType().forEach(businessType->{
                resultMap.put(Integer.valueOf(businessType.toString()), item);
            });
        });
        return resultMap;
    }
    /**
     * 新建
     * @return
     */
    @Bean("businessAddMap")
    public Map<Integer, FormDataService> businessAddMap() {
        Map<Integer, FormDataService> resultMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        this.businessAddMap.forEach((key, item)->{
            item.businessList().forEach(type->resultMap.put(Integer.valueOf(type.toString()), item));
        });
        return resultMap;
    }

    /**
     * 编辑
     * @return
     */
    @Bean("businessUpdateMap")
    public Map<Integer, FormDataUpdateService> businessUpdateMap() {
        Map<Integer, FormDataUpdateService> resultMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        this.businessUpdateMap.forEach((key, item)->{
            item.businessType().forEach(type->resultMap.put(Integer.valueOf(type.toString()), item));
        });
        return resultMap;
    }






}
